package com.bkgy.controller.memcache.service.impl;

import cn.hutool.json.JSONObject;
import com.bkgy.component.StartUpAfterComponent;
import com.bkgy.controller.memcache.service.IMemcacheServer;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.spy.memcached.MemcachedClient;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Service
@Slf4j
@RequiredArgsConstructor
public class MemcacheServiceImpl implements IMemcacheServer {
    private final StartUpAfterComponent memcachedRunner;
    private static Map<String, JSONObject> MEM_MAP = new ConcurrentHashMap<>();
    private static final String PARAM_NAME = "VIB_DATA";

    @Override
    public JSONObject processHandel() {
        JSONObject object = new JSONObject();
        MemcachedClient client = memcachedRunner.getClient();
        if (client == null) {
            log.error("【注意】：memcache客户端不能为空！");
            return object;
        }
        String[] strings = client.get(PARAM_NAME).toString().replace("\u0006", "").split("#");
        for (int i = 0; i < strings.length; i++) {
            object.putOnce((i + 1) + "#_std", strings[i]);
        }
        return object;
    }

//    @Scheduled(fixedRate = 3000)
//    public void processRollingMillVibration() {
//        MemcachedClient client = memcachedRunner.getClient();
//        if (client == null) {
//            return;
//        }
//        String[] strings = client.get(PARAM_NAME).toString().replace("\u0006", "").split("#");
//        JSONObject object = new JSONObject();
//        for (int i = 0; i < strings.length; i++) {
//            object.putOnce((i + 1) + "#_std", strings[i]);
//        }
//        MEM_MAP.put(PARAM_NAME, object);
//    }
}
